package de.justphil.tcg.tcgserver.commons.domain;


import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

import org.codehaus.jackson.map.annotate.JsonSerialize;
import org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion;
import de.justphil.tcg.tcgserver.commons.util.DateTimeUtil;

@Entity
@Table(name = "card_instances", schema = "tcg")
@XmlRootElement
@XmlAccessorType(XmlAccessType.NONE)
public class CardInstance implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4119667729323314596L;

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "card_instance_id")
	@XmlElement
	private long id;
	
	@Column(name = "latitude")
	@XmlElement(name = "lat")
	@JsonSerialize(include = Inclusion.NON_DEFAULT)
	private double latitude;
	
	@Column(name = "longitude")
	@XmlElement(name = "lon")
	@JsonSerialize(include = Inclusion.NON_DEFAULT)
	private double longitude;
	
	// foreign key mapping the associated card
	@Column(name = "card_id")
    @XmlElement(name = "card")
    private long cardId;
	
	// foreign key mapping the owning user
	@Column(name = "user_id")
    private long userId;
	
    //@ManyToMany(mappedBy="cardInstances", fetch=FetchType.EAGER)
	@Transient
    private Set<Long> decks; // Usage of HashSet in order to increase access speed
	
	@Column(name = "created")
	@Temporal(TemporalType.TIMESTAMP)
	private Date created;
	
	@Column(name = "modified")
	@Temporal(TemporalType.TIMESTAMP)
	private Date modified;
	
	
	
	
	
	public CardInstance() {
		// JPA / JAXB requires a default constructor without parameters
		
		this.decks = new HashSet<Long>();
	}
	
	public CardInstance(double latitude, double longitude, long userId, long cardId) {
		this();
		
		this.latitude = latitude;
		this.longitude = longitude;
		this.userId = userId;
		this.cardId = cardId;
		
		long nowUTC = DateTimeUtil.getUTCTimeInMillis();
		this.created = new Date(nowUTC);
		this.modified = new Date(nowUTC);
	}
	

	/* ############################################################################################ */
	/* ############################################################################################ */
	/* ############################################################################################ */
	
	public long getId() {
		return id;
	}

	public double getLatitude() {
		return latitude;
	}

	public double getLongitude() {
		return longitude;
	}
	
	public long getCardId() {
		return cardId;
	}

	public long getUserId() {
		return userId;
	}

	public Set<Long> getDecks() {
		return decks;
	}

	public Date getCreated() {
		return created;
	}

	public Date getModified() {
		return modified;
	}

	public void setId(long id) {
		this.id = id;
	}

	public void setLatitude(double latitude) {
		this.latitude = latitude;
	}

	public void setLongitude(double longitude) {
		this.longitude = longitude;
	}

	public void setCardId(long cardId) {
		this.cardId = cardId;
	}

	public void setUserId(long userId) {
		this.userId = userId;
	}

	public void setDecks(Set<Long> decks) {
		this.decks = decks;
	}

	public void setCreated(Date created) {
		this.created = created;
	}

	public void setModified(Date modified) {
		this.modified = modified;
	}

	
	
	/* ############################################################################################ */
	/* ############################################################################################ */
	/* ############################################################################################ */

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + (int) (id ^ (id >>> 32));
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		CardInstance other = (CardInstance) obj;
		if (id != other.id)
			return false;
		return true;
	}
	
	/* ######################################################################## */
	/* ######################################################################## */
	/* ######################################################################## */

	/*
	@Override
	public void readData(DataInput in) throws IOException {
		id = in.readLong();
		latitude = in.readDouble();
		longitude = in.readDouble();
		created = new Date(in.readLong());
		modified = new Date(in.readLong());
	}

	@Override
	public void writeData(DataOutput out) throws IOException {
		out.writeLong(id);
		out.writeDouble(latitude);
		out.writeDouble(longitude);
		out.writeLong(created.getTime());
		out.writeLong(modified.getTime());
	}
	*/
}
